#!/usr/bin/env python
# coding:utf-8
##最小二乘法
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
x = np.array(list(range(40)))

y = np.array([25, 25, 26, 29, 25, 26, 27, 20, 19, 21, 23, 23, 24, 27, 24, 21, 21, 20, 20, 19, 18, 19, 19, 19, 20, 19, 19, 20, 18, 17, 17, 17, 17, 17, 18, 18, 19, 19, 19, 19])

x1 = list(range(7))
y1 = [25, 25, 26, 29, 25, 26, 27]

#Xi = np.array(list(range(15)))
#Yi= np.array([26, 24, 27, 29, 24, 25, 27, 24, 23, 24, 25, 23, 25, 26, 25])
day = 40

import matplotlib.pyplot as plt
import math
import numpy
import random

fig = plt.figure()
ax = fig.add_subplot(111)

# 阶数为9阶
order = 9

# 生成曲线上的各个点
# x = numpy.arange(-1, 1, 0.02)
# y = [((a * a - 1) * (a * a - 1) * (a * a - 1) + 0.5) * numpy.sin(a * 2) for a in x]
# ax.plot(x,y,color='r',linestyle='-',marker='')
# ,label="(a*a-1)*(a*a-1)*(a*a-1)+0.5"

# 生成的曲线上的各个点偏移一下，并放入到xa,ya中去
i = 0
xa = []
ya = []
for xx in x:
    yy = y[i]
    d = float(random.randint(60, 140)) / 100
    # ax.plot([xx*d],[yy*d],color='m',linestyle='',marker='.')
    i += 1
    xa.append(xx * d)
    ya.append(yy * d)

'''for i in range(0,5):
	xx=float(random.randint(-100,100))/100
	yy=float(random.randint(-60,60))/100
	xa.append(xx)
	ya.append(yy)'''

ax.plot(x, y, color='m')

# 进行曲线拟合
matA = []
for i in range(0, order + 1):
    matA1 = []
    for j in range(0, order + 1):
        tx = 0.0
        for k in range(0, len(xa)):
            dx = 1.0
            for l in range(0, j + i):
                dx = dx * xa[k]
            tx += dx
        matA1.append(tx)
    matA.append(matA1)

# print(len(xa))
# print(matA[0][0])
matA = numpy.array(matA)

matB = []
for i in range(0, order + 1):
    ty = 0.0
    for k in range(0, len(xa)):
        dy = 1.0
        for l in range(0, i):
            dy = dy * xa[k]
        ty += ya[k] * dy
    matB.append(ty)

matB = numpy.array(matB)

matAA = numpy.linalg.solve(matA, matB)

# 画出拟合后的曲线
# print(matAA)
xxa = x
yya = []
for i in range(0, len(xxa)):
    yy = 0.0
    for j in range(0, order + 1):
        dy = 1.0
        for k in range(0, j):
            dy *= xxa[i]
        dy *= matAA[j]
        yy += dy
    yya.append(yy)
ax.plot(xxa, yya, color='g', linestyle='-', marker='')

ax.legend()
plt.show()


